// pages/orderControl/orderControl.js
import { onlineOrderList, shippingCompany, goDelivery, getProductState, orderDel, lookOrderRemark, editOrderRemark} from '../../api.js'
let app = getApp();
Page({
  data: {
    checkedId: 0,
    hasOnlineOrder:true, //默认空白页面不显示
    orderStatus: [
      { id: 0, value: '全部订单' },
      { id: 1, value: '待付款' },
      { id: 2, value: '待发货' },
      { id: 3, value: '待收货' },
      { id: 4, value: '已完成' },
    ],
    onlineOrderList: [], //门店订单列表
    goDelivery:{
      shipping_code:'', //物流公司
      shipping_sn:'', //物流公司单号
      batchcode:'' ,//商品号
      info_batchcode:'' //订单详情号
    },
    deliveryCompany:[],//快递公司
    shipping_name:'', //快递公司名称
    isShowDelivery:false,
    page:1,
    last_page:1,
    showToast:false,
    remarkInfo: '', //备注信息
    showRemarkDialog: false,
    remarkBatchcode: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function(options){
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    shippingCompany({token:app.globalData.token}).then(res=>{
      if(res.data.code==666){
        this.setData({
          deliveryCompany: res.data.data
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      }else{
        wx.showToast({
          title: res.data.msg,
          icon:'none'
        })
      }
      this.orderData();
    })
  },
  onShow(){
  },
  onHide(){
    this.setData({
      showToast:false,
      isShowDelivery: false,
    })
  },
  orderData(){
    var _this=this;
    onlineOrderList({ token: app.globalData.token, page:1, type:3, status: _this.data.checkedId }).then(res => {
      if(res.data.code==666){
        _this.setData({
          page: res.data.data.current_page,
          last_page: res.data.data.last_page,
          onlineOrderList: res.data.data.data,
        })
        _this.data.onlineOrderList.forEach(function(item){        
          item.total_price = item.total_price ? item.total_price.toFixed(2):0
        })
        _this.setData({
          onlineOrderList: _this.data.onlineOrderList
        })
        wx.hideLoading();
        if(res.data.data.data.length==0){
          _this.setData({
            hasOnlineOrder:false
          })
        }
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      }else{
        wx.showToast({
          title: res.data.msg,
          icon:"none"
        })
      }

    })
  },
  selectedShipping(){
    this.setData({
      isShowDelivery:true
    })
  },
  selectedCom(e) {
    console.log('e', e)
    this.setData({
      'goDelivery.shipping_code':e.currentTarget.dataset.id,
      shipping_name: e.currentTarget.dataset.company,
      isShowDelivery: false
    })
  },
  checkedStatus(e) {
    console.log('ee',e.currentTarget.dataset.id)
    this.setData({
      checkedId: e.currentTarget.dataset.id
    })
    this.onlineOrder();
  },
  onlineOrder(){
    wx.showLoading({
      title: '加载中',
    })
    var _this=this;
    _this.setData({
      hasOnlineOrder:true
    })
    onlineOrderList({ token: app.globalData.token, page:_this.data.page, type: 3, status: _this.data.checkedId }).then(res => {
      if(res.data.code==666){
        wx.hideLoading();
        _this.setData({
          page: res.data.data.current_page,
          last_page: res.data.data.last_page,
          onlineOrderList: res.data.data.data
        })
        if (res.data.data.data.length == 0) {
          _this.setData({
            hasOnlineOrder: false
          })
        }
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      }else{
        wx.hideLoading();
        wx.showToast({
          title: res.data.msg,
        })
      }

    })
  },
  fuzhi(e){
    let content = e.target.dataset.con
    wx.setClipboardData({
      data: content,
      success() {
        wx.showToast({
          title: '复制成功',
        })
      }
    })
    console.log('content', content);
  },
  goDelivery(e) {
    console.log('e',e)
    this.setData({
      showToast:true,
      'goDelivery.batchcode': e.currentTarget.dataset.batchcode,
      'goDelivery.info_batchcode': e.currentTarget.dataset.info_batchcode,
      'goDelivery.shipping_sn':'',
      shipping_name:'',
    })
  },
  lookOrder(e){
    console.log(e.currentTarget.dataset.batchcode)
    wx.navigateTo({
      url: '/pages/orderControl/orderDetail/orderDetail?id=' + e.currentTarget.dataset.id + '&index=' + e.currentTarget.dataset.index + '&index2=' + e.currentTarget.dataset.index2 + '&info_batchcode=' + e.currentTarget.dataset.batchcode,
    })
  },
  shippingSn(e){
    console.log('物流单号',e)
    this.setData({
      'goDelivery.shipping_sn':e.detail.value
    })
  },
  cancelDelivery(){
    this.setData({
      showToast: false,
      isShowDelivery: false
      
    })
  },
  confirmDelivery(){
    let goDeliveryInfo = this.data.goDelivery;
    goDeliveryInfo.token=app.globalData.token;
    goDelivery(goDeliveryInfo).then(res=>{
      if(res.data.code==666){
        wx.showToast({
          title: res.data.msg,
          icon: 'success',
        })
        this.onlineOrder()
        this.setData({
          showToast: false,
          isShowDelivery: false,
          'goDelivery.shipping_code':'',
          'goDelivery.shipping_sn': '',
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      }else{
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
        })
      }
    })
  },
  // 修改人:邵群
  // 修改日期：2018.11.05
  // 修改内容：删除订单
  del(e) {
    console.log('e', e)
    var _this = this;
    let info_batchcode = e.currentTarget.dataset.batchcode;
    let index = e.currentTarget.dataset.index;
    let index2 = e.currentTarget.dataset.index2;
    wx.showModal({
      title: '删除订单',
      content: '是否确认删除该订单？',
      showCancel: true,
      confirmColor: '#000',
      success(res) {
        if (res.confirm) {
          orderDel({ token: app.globalData.token, info_batchcode }).then(res => {
            _this.data.onlineOrderList[index].product_data.splice(index2, 1)
            _this.setData({
              onlineOrderList: _this.data.onlineOrderList
            })
            if (_this.data.onlineOrderList[index].product_data.length==0){
              console.log('length', _this.data.onlineOrderList[index].product_data.length)
              _this.data.onlineOrderList.splice(index,1)
              _this.setData({
                onlineOrderList: _this.data.onlineOrderList
              })
            }
          })
        }
      }
    })
  },
  onReachBottom: function () {
    var _this=this;
    if (_this.data.page == _this.data.last_page) {
      wx.showToast({
        title: '订单已全部加载',
        icon: 'success',
        mask: true,
      })
    } else {
      wx.showLoading({
        title: '加载中',
        mask: true
      })
        onlineOrderList({ token: app.globalData.token, page:++_this.data.page, type: 3, status: _this.data.checkedId }).then(res => {
          if(res.data.code==666){
            wx.hideLoading();
            _this.setData({
              page: res.data.data.current_page,
              last_page: res.data.data.last_page,
              onlineOrderList: _this.data.onlineOrderList.concat(res.data.data.data) 
            })
          } else if (res.data.code == 111111) {
            wx.showToast({
              title: '登录失效，请重新登录',
              icon: 'none',
            })
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/user/login/login',
              })
            }, 1500)
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
            })
          }
        })
    }
  },
  onTabItemTap:function(){
    wx.showLoading({
      title: '正在加载...',
    })
    this.setData({
      onlineOrderList:''
    })
    this.orderData()
  },
  // 修改人:颜阁
  // 修改日期：2018.10.29
  // 修改内容：增加扫描系统
  scanning:function(){
    var that = this
    wx.showLoading({
      title: '正在扫描',
    })
    wx.scanCode({
      success(res) {
        wx.hideLoading()
        wx.showToast({
          title: '扫描成功',
        })
        that.data.goDelivery.shipping_sn = res.result
        that.setData({
          goDelivery: that.data.goDelivery,
          showToast: true
        })
      },fail(){
        wx.hideLoading()
        wx.showToast({
          title: '扫描失败',
          icon:'none'
        })
      }
    }) 
  },

  /**  
 * 修改人：邵群
 * 修改时间：2018-11 - 27
 * 修改内容：添加留言编辑修改功能
*/
remarkDialog(e) {
  let batchcode = e.currentTarget.dataset.batchcode;
    this.setData({
      remarkBatchcode: batchcode,
      showRemarkDialog: true,
    })
    lookOrderRemark({ batchcode: batchcode, token: app.globalData.token }).then(res => {
      if (res.data.code == 666) {
        this.setData({
          remarkInfo: res.data.data
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  remarkCon(e) {
    console.log(e);
    this.setData({
      remarkInfo: e.detail.value
    })
  },
  remarkConfirm(e) {
    let _this = this;
    if (e.currentTarget.dataset.flag == 'true') {
      editOrderRemark({ token: app.globalData.token, batchcode: _this.data.remarkBatchcode, remark: _this.data.remarkInfo }).then(res => {
        if (res.data.code == 666) {
          _this.setData({
            showRemarkDialog: false,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      })
    } else {
      _this.setData({
        showRemarkDialog: false,
      })
    }
  },
  lisGO: function (e) {
    var batchcode = e.currentTarget.dataset.batchcode
    wx.navigateTo({
      url: '/pages/user/afterSale/afterSaleLis/afterSaleLis?batchcode=' + batchcode
    })
  }
})
